package com.frame.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.BasicErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.google.common.base.Predicate;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * SwaggerConfig
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")
public class SwaggerConfig extends WebMvcConfigurerAdapter{

	@Value("${swagger.api.base.package}")
	private String basePackage;
	



	private Predicate<RequestHandler> getPredicate(final String packageName){
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                Class<?> declaringClass = input.declaringClass();
                if(declaringClass.getPackage().getName().startsWith(packageName)){
                    // 排除
                    if (declaringClass == BasicErrorController.class){
                        return false;
                    }
                    // 被注解的类
                    if(declaringClass.isAnnotationPresent(RestController.class)){
                        return true;
                    }
                    // 被注解的方法
                    if(input.isAnnotatedWith(ResponseBody.class)){
                        return true;
                    }
                    return true;
                }
                return false;
            }
        };
        return predicate;
    }

    @Bean
    public Docket createRestApi() {


        
        
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("系统API")
                .apiInfo(apiInfo("系统接口文档"))
                .useDefaultResponseMessages(false)
                .select()
                .apis(getPredicate(basePackage))
                .build();
    }

    private ApiInfo apiInfo(String description) {
        return new ApiInfoBuilder()
            //大标题
            .title("API文档")
            //详细描述
            .description(description)
            //版本
            .version("1.0.0")
            //条款服务网址
            .termsOfServiceUrl("http://about.yidaba.com/service.htm")
            //作者信息
            .contact(contact())
            .build();
    }

    /**
     * description:设置作者信息
     * @author: Flying
     * @date: 2018-08-31 15:01
     * @param: null
     * @return
     */
    private Contact contact() {
        return new Contact("Flying", "https://my.oschina.net/flyingmiku/blog", "363131422@qq.com");
    }

}